package A1_回溯;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class LC_47全排列II {
     LinkedList<Integer> path=new LinkedList<>();
     List<List<Integer>> result=new ArrayList<>();
    public  List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        int[] used=new int[nums.length];
        backtracking(nums,used);
        return result;
    }
    public  void backtracking(int[] nums, int[] used){
        if(path.size()==nums.length){
            result.add(new ArrayList<>(path));
            return;
        }
        for(int i=0;i<nums.length;i++){
            if(used[i]==1)
                continue;
            if(i>0 && nums[i]==nums[i-1] && used[i-1]==0)
                continue;
            used[i]=1;
            path.add(nums[i]);
            backtracking(nums,used);
            used[i]=0;
            path.removeLast();
        }
        return;
    }
    public static void main(String[] args){
        int[] nums={2,1,1};
//        permuteUnique(nums);
    }
}
